package com.ticket.system.service.impl;

import com.google.common.base.Joiner;
import com.ticket.common.core.domain.entity.SysDept;
import com.ticket.common.exception.base.BaseException;
import com.ticket.system.domain.YanchuHall;
import com.ticket.system.mapper.SysDeptMapper;
import com.ticket.system.mapper.YanchuHallMapper;
import com.ticket.system.service.IYanchuHallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 场馆Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-26
 */
@Service
public class YanchuHallServiceImpl implements IYanchuHallService 
{
    @Autowired
    private YanchuHallMapper yanchuHallMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询场馆
     * 
     * @param id 场馆主键
     * @return 场馆
     */
    @Override
    public YanchuHall selectYanchuHallById(Long id)
    {
        return yanchuHallMapper.selectYanchuHallById(id);
    }

    /**
     * 查询场馆列表
     * 
     * @param yanchuHall 场馆
     * @return 场馆
     */
    @Override
    public List<YanchuHall> selectYanchuHallList(YanchuHall yanchuHall)
    {
        return yanchuHallMapper.selectYanchuHallList(yanchuHall);
    }

    @Override
    public List<YanchuHall> selectChildHallList(Long deptId)
    {
        List<SysDept> list = deptMapper.operaList(deptId,null,1l);
        if(!CollectionUtils.isEmpty(list)){
            String array = Joiner.on(",").join(list.stream().map(SysDept::getDeptId).collect(Collectors.toList()));
            return yanchuHallMapper.selectChildHallList(array);
        }
        return null;
    }

    /**
     * 新增场馆
     * 
     * @param yanchuHall 场馆
     * @return 结果
     */
    @Override
    public int insertYanchuHall(YanchuHall yanchuHall)
    {
        //校验当前剧院下是否存在该名称
        if (chechName(null,yanchuHall.getDeptId(),yanchuHall.getHallName())){
            throw new BaseException("剧场名称已存在");
        }
        return yanchuHallMapper.insertYanchuHall(yanchuHall);
    }

    /**
     * 修改场馆
     * 
     * @param yanchuHall 场馆
     * @return 结果
     */
    @Override
    public int updateYanchuHall(YanchuHall yanchuHall)
    {
        if (chechName(yanchuHall.getId(),yanchuHall.getDeptId(),yanchuHall.getHallName())){
            throw new BaseException("剧场名称已存在");
        }
        return yanchuHallMapper.updateYanchuHall(yanchuHall);
    }

    private Boolean chechName(Long id, Long deptId, String hallName) {
        YanchuHall hall = new YanchuHall();
        hall.setDeptId(deptId);
        List<YanchuHall> halls = yanchuHallMapper.selectYanchuHallList(hall);
        if (CollectionUtils.isEmpty(halls)){
            return false;
        }
        if (null != id){
            //编辑
            for (YanchuHall yanchuHall : halls) {
                if (hallName.equals(yanchuHall.getHallName())){
                    if (!id.equals(yanchuHall.getId())){
                        return true;
                    }
                }
            }
        }else {
            //新增
            if (halls.stream().map(YanchuHall::getHallName).collect(Collectors.toList()).contains(hallName)){
                return true;
            }
        }
        return false;
    }

    /**
     * 批量删除场馆
     * 
     * @param ids 需要删除的场馆主键
     * @return 结果
     */
    @Override
    public int deleteYanchuHallByIds(Long[] ids)
    {
        return yanchuHallMapper.deleteYanchuHallByIds(ids);
    }

    /**
     * 删除场馆信息
     * 
     * @param id 场馆主键
     * @return 结果
     */
    @Override
    public int deleteYanchuHallById(Long id)
    {
        return yanchuHallMapper.deleteYanchuHallById(id);
    }
}
